package com.example.drss;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.example.drss.adapter.GridViewAdapter;
import com.example.drss.bean.Menu;
import com.example.drss.db.DBTool;
import com.example.drss.itemdata.ItemData;

public class SellerActivity extends Activity{
	TextView sellerNameView;
	String path;
	String sellerName;
	DBTool dbTool;
	List<ItemData> data;
	Cursor cursor;
	GridView  gridView;
	Button backButton;
	TextView textView;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seller);
		Intent intent=getIntent();
		path=intent.getStringExtra("databasePath_seller");
		sellerName=intent.getStringExtra("seller_name");
		//Toast.makeText(SellerActivity.this, path, Toast.LENGTH_SHORT).show();
		dbTool=new DBTool(path);
		dbTool.openDatabase();
		init();
	}
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dbTool.closeDatabase();
	}

	private void init() {
		// TODO Auto-generated method stub
		sellerNameView=(TextView)findViewById(R.id.textView_seller);
		sellerNameView.setText(sellerName);
		gridView=(GridView)findViewById(R.id.gridView_seller);
		backButton=(Button)findViewById(R.id.button_seller_back);
		textView=(TextView)findViewById(R.id.textView_null);

		data = new ArrayList<ItemData>();
		cursor=dbTool.queryBySellerName(sellerName);
		if (cursor!=null) {
			//读取所有纪录
			List<Menu> menus =new ArrayList<Menu>();
			while(cursor.moveToNext()){
				Menu menu=new Menu();
				menu.setName(cursor.getString(1));
				menu.setImage(cursor.getString(2));
				menus.add(menu);
			}
			for(Menu menu:menus){
				ItemData data1= new ItemData();
				data1.setName(menu.getName());
				data1.setImage(menu.getImage());
				data.add(data1);
			}	
			GridViewAdapter gridViewAdapter=new GridViewAdapter(SellerActivity.this, data);
			gridView.setAdapter(gridViewAdapter);	
		}
		
		else {
			textView.setText("null");
		}

		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				//onDestroy();
				dbTool.closeDatabase();
			}
		});

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(SellerActivity.this,DishesActivity.class);
				ItemData itemData=data.get(position);
				intent.putExtra("name", itemData.getName());
				intent.putExtra("databasePath_dishes", path);
				startActivity(intent);
			}
		});
	}

}
